﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System;

public class MyTools : Editor
{
	/// <summary>
	/// 查找挂有对应组件的gameobject
	/// </summary>
	[MenuItem("My Tools/Find object with component", false, 11)]
	public static void FindObject()
	{
       
		Rect wr = new Rect(0, 0, 500, 200);
		FindObjectWithComponent window = (FindObjectWithComponent)EditorWindow.GetWindowWithRect(typeof(FindObjectWithComponent), wr, true, "Find Object Tool");
		window.Show();
	}

	/// <summary>
	/// 清除editor pc的游戏存档
	/// </summary>
	[MenuItem("My Tools/Clear PlayerPrefs in this project")]
	public static void ClearPlayerPrefs()
	{
		ClearPlayerPref.Clear();
	}
}

public class ClearPlayerPref
{
	private static string cName;
	private static string pName;
	public static void Clear()
	{
		cName = PlayerSettings.companyName;
		pName = PlayerSettings.productName;
		string t = @"SOFTWARE\" + cName + @"\" + pName;
		try
		{
			Microsoft.Win32.RegistryKey keylm = Microsoft.Win32.Registry.CurrentUser;
			Microsoft.Win32.RegistryKey target = keylm.OpenSubKey(t, true);
			var valueList = target.GetValueNames();
			for (int i = 0; i < valueList.Length; i++) { target.DeleteValue(valueList[i]); }
		}
		catch(Exception exc)
		{
			Debug.Log(">>>>>the exception in the clear playerpref:" + exc.Message);
		}
	}
}

public class FindObjectWithComponent:EditorWindow
{
	//输入文字的内容
	private string text;
	//private string temp;
	//选择贴图的对象
	private Texture texture;
	private List<GameObject> golist;
	private string t = "";
	void OnGUI()
	{
		EditorGUILayout.LabelField("查找挂了某组件的GameObject");
		//输入框控件
		text = EditorGUILayout.TextField("输入组件名字:", text);
		
		if (GUILayout.Button("查找", GUILayout.Width(200)))
		{ 
			var goList = FindObjectsOfType<GameObject>();
			golist = new List<GameObject>();
			foreach (var item in goList)
			{
				if (item.GetComponent(text))
				{
					item.GetComponent(text);
					golist.Add(item);
					t += item.name + "\n";
				}
			}
			//Debug.Log(">>>>" + golist.Count);
		}
		EditorGUILayout.BeginVertical();
		EditorGUILayout.LabelField("object:");
		EditorGUILayout.TextArea(t);

        if (GUILayout.Button("打开通知", GUILayout.Width(200)))
        {
            //打开一个通知栏
            this.ShowNotification(new GUIContent("This is a Notification"));
        }

        if (GUILayout.Button("关闭通知", GUILayout.Width(200)))
        {
            //关闭通知栏
            this.RemoveNotification();
        }

        //文本框显示鼠标在窗口的位置
        EditorGUILayout.LabelField("鼠标在窗口的位置", Event.current.mousePosition.ToString());

        //选择贴图
        texture = EditorGUILayout.ObjectField("添加贴图", texture, typeof(Texture), true) as Texture;

        if (GUILayout.Button("关闭窗口", GUILayout.Width(200)))
		{
			//关闭窗口
			this.Close();
		}
	}
}
